package com.xgg.taiwuinfo.filter.fc;

import android.util.Log;

import com.xgg.taiwuinfo.filter.FC_SelectCompareSelect;
import com.xgg.taiwuinfo.filter.FilterComponent;
import com.xgg.taiwuinfo.gongfa.base.Gongfa;
import com.xgg.taiwuinfo.gongfa.base.Gongfa_Neigong;
import com.xgg.taiwuinfo.utils.GongfaType;

import org.json.JSONObject;

import java.util.ArrayList;

public class FCNeigongGridCompareSelect extends FC_SelectCompareSelect {

    public FCNeigongGridCompareSelect(JSONObject obj)
    {
        mTypeId = FilterComponent.FT_NEIGONGGRID;
        if (obj != null) {
            try {
                ReadField = obj.getInt("f");
                ReadCompare = obj.getInt("c");
                ReadValue = obj.getInt("v");
            } catch (Exception e) {
                Log.d("LoadJson", e.getLocalizedMessage());
                ReadField = 0;
                ReadCompare = FC_GREATER;
                ReadValue = 0;
            }
        }
        else {
            ReadField = 0;
            ReadCompare = FC_GREATER;
            ReadValue = 0;
        }
        TempField = ReadField;
        TempValue = ReadValue;
        TempCompare = ReadCompare;

        if (mFieldMap == null) {
            mFieldMap = new ArrayList<>();
            AddMap(mFieldMap, "总格数", "0");
            AddMap(mFieldMap, "摧破格数", "1");
            AddMap(mFieldMap, "摧破格数（含万用）", "2");
            AddMap(mFieldMap, "轻灵格数", "3");
            AddMap(mFieldMap, "轻灵格数（含万用）", "4");
            AddMap(mFieldMap, "护体格数", "5");
            AddMap(mFieldMap, "护体格数（含万用）", "6");
            AddMap(mFieldMap, "奇窍格数", "7");
            AddMap(mFieldMap, "奇窍格数（含万用）", "8");
            AddMap(mFieldMap, "万用格数", "9");
        }

        if (mValueMap == null) {
            mValueMap = new ArrayList<>();
            AddMap(mValueMap , "1" , "1");
            AddMap(mValueMap , "2" , "2");
            AddMap(mValueMap , "3" , "3");
            AddMap(mValueMap , "4" , "4");
            AddMap(mValueMap , "5" , "5");
            AddMap(mValueMap , "6" , "6");
            AddMap(mValueMap , "7" , "7");
            AddMap(mValueMap , "8" , "8");
            AddMap(mValueMap , "9" , "9");
            AddMap(mValueMap , "10" , "10");
            AddMap(mValueMap , "20" , "20");
        }

    }

    @Override
    public String FilterGongfa(Gongfa gongfa){
        if (gongfa.mType != GongfaType.TYPE_NEIGONG)
            return null;
        try {
            Gongfa_Neigong ng = (Gongfa_Neigong) gongfa;
            int tarvalue = 0;
            String fmt = "%d";
            switch (ReadField)
            {
                case 0:
                    tarvalue = ng.getGridCuipo() + ng.getGridQingling() + ng.getGridHuti() + ng.getGridQiqiao() + ng.getGridWanyong();
                    fmt = "总格数 : %d";
                    break;
                case 1:
                    tarvalue = ng.getGridCuipo();
                    fmt = "摧破格数 : %d";
                    break;
                case 2:
                    tarvalue = ng.getGridCuipo() + ng.getGridWanyong();
                    fmt = "摧破含万用格数 : %d";
                    break;
                case 3:
                    tarvalue = ng.getGridQingling();
                    fmt = "轻灵格数 : %d";
                    break;
                case 4:
                    tarvalue = ng.getGridQingling() + ng.getGridWanyong();
                    fmt = "轻灵含万用格数 : %d";
                    break;
                case 5:
                    tarvalue = ng.getGridHuti();
                    fmt = "护体格数 : %d";
                    break;
                case 6:
                    tarvalue = ng.getGridHuti() + ng.getGridWanyong();
                    fmt = "护体含万用格数 : %d";
                    break;
                case 7:
                    tarvalue = ng.getGridQiqiao();
                    fmt = "奇窍格数 : %d";
                    break;
                case 8:
                    tarvalue = ng.getGridQiqiao() + ng.getGridWanyong();
                    fmt = "奇窍含万用格数 : %d";
                    break;
                case 9:
                    tarvalue = ng.getGridWanyong();
                    fmt = "万用格数 : %d";
                    break;
            }
            switch (ReadCompare)
            {
                case FC_GREATER:
                    if (tarvalue > ReadValue)
                        return String.format(fmt , tarvalue);
                    else
                        return null;
                case FC_GREATERCONTAIN:
                    if (tarvalue >= ReadValue)
                        return String.format(fmt , tarvalue);
                    else
                        return null;
                case FC_EQUAL:
                    if (tarvalue == ReadValue)
                        return String.format(fmt , tarvalue);
                    else
                        return null;
                case FC_SMALLERCONTAIN:
                    if (tarvalue <= ReadValue)
                        return String.format(fmt , tarvalue);
                    else
                        return null;
                case FC_SMALLER:
                    if (tarvalue < ReadValue)
                        return String.format(fmt , tarvalue);
                    else
                        return null;
                default:
                    return null;
            }
        }
        catch (Exception e) {
            return null;
        }
    }
}
